#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 2e4 + 5;
int fa[N], rk[N];
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
inline void merge(int a, int b)
{
	a = find(a), b = find(b);
	if (a == b)return;
	if (rk[a] > rk[b])
		fa[b] = a;
	else fa[a] = b;
	if (rk[a] == rk[b])
		rk[b]++;
}
int main()
{
	cin.sync_with_stdio(false);
	int n, m, p, q;
	cin >> n >> m >> p >> q;
	while (p--)
	{
		int a, b;
		cin >> a >> b;
		merge(a, b);
	}
	int ans1 = 0, ans2 = 0;
	while (q--)
	{
		int a, b;
		cin >> a >> b;
		a = -a + n, b = -b + n;
		merge(a, b);
	}
	for (int i = 1; i <= n + m; i++)
	{
		if (find(i) == 1)ans1++;
		else if (find(i) == 1 + n)ans2++;
	}
	cout << min(ans1, ans2);
	return 0;
}
